package ted

import (
	"Readme/inits"
	"Readme/model"
)

func Qun(user_id int, name string) (qun model.Qun, err error) {
	qun = model.Qun{

		UserId: user_id,
		Name:   name,
	}
	err = inits.DB.Create(&qun).Error
	if err != nil {
		return model.Qun{}, err
	}
	return qun, nil
}

func Group(i int, user_id int) (group model.Group, err error) {
	group = model.Group{

		UserId: user_id,
		Type:   3,
	}
	err = inits.DB.Debug().Create(&group).Error
	if err != nil {
		return model.Group{}, err
	}
	return group, err
}

func DeltedGroup(i int, id int) (grout model.Group, err error) {
	err = inits.DB.Where("user_id=?", i).Find(&grout).Error
	if err != nil {
		return model.Group{}, err
	}
	if grout.Type >= 3 {
		return model.Group{}, err
	}
	err = inits.DB.Model(&grout).Delete("user_id=?", id).Error
	if err != nil {
		return model.Group{}, err
	}
	return grout, err
}

func UpdateGroup(i int, id int) (grout model.Group, err error) {
	err = inits.DB.Where("user_id=?", i).Find(&grout).Error
	if err != nil {
		return model.Group{}, err
	}
	if grout.Type >= 3 {
		return model.Group{}, err
	}
	err = inits.DB.Model(&grout).Where("user_id=?", id).Update("type", 2).Error
	if err != nil {
		return model.Group{}, err
	}
	return grout, err
}
